package com.zz.retryall.service;

import com.zz.retryall.exception.HelloRetryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class HelloRetryProxyService implements IHelloService{
   
    @Autowired
    private HelloRetryService helloRetryService;


    /**
     * 简单封装的代理，可以把变化封装到这里避免变化蔓延到实际业务服务
     *
     * @return
     */
    @Override
    public String hello() {
        int maxRetryTimes = 4;
        String s;
        for (int i = 1; i <= maxRetryTimes; i++) {
            try {
                s = helloRetryService.hello();
                log.info("helloRetryService 返回:{}", s);
                return s;
            } catch (HelloRetryException e) {
                log.info("第{}次调用helloService.hello()失败，准备重试", i);
            }
        }

        //sleep
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        throw new HelloRetryException("重试次数耗尽");
    }
}
